/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.web;

import com.imenu.ejb.ImenuMenuFacade;
import com.imenu.entity.ImenuMenu;
import com.imenu.entity.ImenuUser;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "MenuServlet", urlPatterns = {"/menu"})
public class MenuServlet extends BaseServlet {

    @EJB
    ImenuMenuFacade menuFacade;

    void menuList(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ImenuUser user = (ImenuUser) request.getSession().getAttribute("userInSession");
        List<ImenuMenu> menuList = menuFacade.findByStore(user.getStore());
        System.out.println("MenuServlet.menuList() menuList.size() = " + menuList.size());

        for (ImenuMenu menu : menuList) {
            System.out.println("MenuServlet.menuList() menu.getImageLandscape() = " + menu.getImageLandscape());
            System.out.println("MenuServlet.menuList() menu.getSmallPortrait() = " + menu.getSmallPortrait());
        }

        request.setAttribute("menuList", menuList);
        System.out.println("MenuServlet.menuList() user = " + user);
        forward(request, response, "/menu/menuList.jsp");
    }

    void menuInfo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String menuIdParam = request.getParameter("menuId");
        ImenuMenu menu = null;
        if (menuIdParam != null) {
            Integer menuId = Integer.valueOf(menuIdParam);
            menu = menuFacade.find(menuId);
        } else {
            menu = new ImenuMenu();
        }
        request.setAttribute("menu", menu);
        forward(request, response, "/menu/menuInfo.jsp");
    }

    void save(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String menuIdParam = request.getParameter("menuId");
        if (menuIdParam == null || menuIdParam.length() == 0) {
            ImenuMenu menu = new ImenuMenu();
            this.parseRequest(request, menu);
            ImenuUser user = (ImenuUser) request.getSession(true).getAttribute("userInSession");
            menu.setStore(user.getStore());
            System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MenuServlet.save() create menu =" + menu);
            System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MenuServlet.save() create menu.getId() =" + menu.getId());
            System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MenuServlet.save() create menu.getName() =" + menu.getName());
            System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MenuServlet.save() create menu.getTitle() =" + menu.getTitle());
            System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MenuServlet.save() create menu.getStore() =" + menu.getStore());
            System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MenuServlet.save() create menu.getDescription() =" + menu.getDescription());
            menu.setUpdateTime(new Date(System.currentTimeMillis()));
            menuFacade.create(menu);
        } else {
            ImenuMenu menu = menuFacade.find(Integer.valueOf(menuIdParam));
            System.out.println("MenuServlet.save() edit menu =" + menu);
            this.parseRequest(request, menu);
            menuFacade.edit(menu);
        }
        this.forward(request, response, "/menu?command=menuList");
    }

    void unknown(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<ImenuMenu> menuList = menuFacade.findAll();
        request.setAttribute("menuList", menuList);
        forward(request, response, "/menu/unknown.jsp");
    }
}
